﻿using System;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
namespace TVersityDotNet.Requests
{
    public class ProxyRequest : TVersityRequest
    {
        private TVersityRequest _request;
        private Uri _baseUrl;

        /// <summary>
        /// Proxy a request
        /// Intent is to create a new TVersityResponse type (if the base TVersityResponse isn't the expected one)
        /// giving the response XML from this request's TVersityResponse.
        /// </summary>
        /// <param name="request">The TVersityRequest to proxy</param>
        /// <param name="baseUrl">The TVersity Server's baseUrl</param>
        /// <exception cref="InvalidOperationException"/>        
        public ProxyRequest(TVersityRequest request, Uri baseUrl)
        {
            this._baseUrl = baseUrl;
            base.Path = "general/proxy";

            if (request != null)
            {
                if (request.GetType() == this.GetType())
                {                    
                    throw new InvalidOperationException("Could not load proxy: TVersityRequest cannot be of type ProxyRequest");
                }
                else
                {                    
                    this._request = request;                    
                }
            }
            else
            {                
                this._request = new TVersityRequest();                
            }            
        }

        /// <summary>
        /// Returns the whole string, minus the baseurl used to process this query
        /// </summary>
        public override string RequestString
        {
            get
            {
                return string.Format(CultureInfo.InvariantCulture, "{0}?url={1}{2}", base.RequestString, this._baseUrl.AbsoluteUri, this._request.RequestString);
            }
        }        
    }
}
